package com.qianying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianying.comment.CommonResult;
import com.qianying.entity.OrderType;
import com.qianying.mapper.OrderTypeMapper;
import com.qianying.service.OrderTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 订单类型Service实现类
 */
@Service
public class OrderTypeServiceImpl extends ServiceImpl<OrderTypeMapper, OrderType> implements OrderTypeService {

    @Autowired
    private OrderTypeMapper orderTypeMapper;

    @Override
    public CommonResult<IPage<OrderType>> getPage(Page<OrderType> page, String orderTypeName) {
        LambdaQueryWrapper<OrderType> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(orderTypeName)) {
            queryWrapper.like(OrderType::getOrderTypeName, orderTypeName);
        }
        // 按ID升序排列
        queryWrapper.orderByAsc(OrderType::getId);
        IPage<OrderType> resultPage = orderTypeMapper.selectPage(page, queryWrapper);
        return CommonResult.success(resultPage);
    }

    @Override
    public CommonResult<OrderType> getById(Long id) {
        if (id == null) {
            return CommonResult.failed("ID不能为空");
        }
        OrderType orderType = orderTypeMapper.selectById(id);
        if (orderType == null) {
            return CommonResult.failed("未找到ID为" + id + "的订单类型");
        }
        return CommonResult.success(orderType);
    }

    @Override
    public CommonResult<?> saveType(OrderType orderType) {
        // 校验必填项
        if (!StringUtils.hasText(orderType.getOrderTypeName())) {
            return CommonResult.failed("订单类型名称不能为空");
        }
        if (!StringUtils.hasText(orderType.getOrderTypeCode())) {
            return CommonResult.failed("订单类型代号不能为空");
        }
        orderType.setId(null);
        // 校验名称唯一性
        LambdaQueryWrapper<OrderType> nameWrapper = new LambdaQueryWrapper<>();
        nameWrapper.eq(OrderType::getOrderTypeName, orderType.getOrderTypeName());
        if (orderTypeMapper.selectCount(nameWrapper) > 0) {
            return CommonResult.failed("订单类型名称已存在");
        }

        // 校验代号唯一性
        LambdaQueryWrapper<OrderType> codeWrapper = new LambdaQueryWrapper<>();
        codeWrapper.eq(OrderType::getOrderTypeCode, orderType.getOrderTypeCode());
        if (orderTypeMapper.selectCount(codeWrapper) > 0) {
            return CommonResult.failed("订单类型代号已存在");
        }

        int rows = orderTypeMapper.insert(orderType);
        return rows > 0 ? CommonResult.success("新增订单类型成功") : CommonResult.failed("新增订单类型失败");
    }

    @Override
    public CommonResult<?> updateType(OrderType orderType) {
        // 校验必填项
        if (orderType.getId() == null) {
            return CommonResult.failed("ID不能为空");
        }
        if (!StringUtils.hasText(orderType.getOrderTypeName())) {
            return CommonResult.failed("订单类型名称不能为空");
        }
        if (!StringUtils.hasText(orderType.getOrderTypeCode())) {
            return CommonResult.failed("订单类型代号不能为空");
        }

        // 检查记录是否存在
        OrderType existing = orderTypeMapper.selectById(orderType.getId());
        if (existing == null) {
            return CommonResult.failed("未找到ID为" + orderType.getId() + "的订单类型");
        }

        // 校验名称唯一性（排除当前记录）
        LambdaQueryWrapper<OrderType> nameWrapper = new LambdaQueryWrapper<>();
        nameWrapper.eq(OrderType::getOrderTypeName, orderType.getOrderTypeName())
                .ne(OrderType::getId, orderType.getId());
        if (orderTypeMapper.selectCount(nameWrapper) > 0) {
            return CommonResult.failed("订单类型名称已存在");
        }

        // 校验代号唯一性（排除当前记录）
        LambdaQueryWrapper<OrderType> codeWrapper = new LambdaQueryWrapper<>();
        codeWrapper.eq(OrderType::getOrderTypeCode, orderType.getOrderTypeCode())
                .ne(OrderType::getId, orderType.getId());
        if (orderTypeMapper.selectCount(codeWrapper) > 0) {
            return CommonResult.failed("订单类型代号已存在");
        }

        int rows = orderTypeMapper.updateById(orderType);
        return rows > 0 ? CommonResult.success("更新订单类型成功") : CommonResult.failed("更新订单类型失败");
    }

    @Override
    public CommonResult<?> removeById(Long id) {
        if (id == null) {
            return CommonResult.failed("ID不能为空");
        }
        // 检查记录是否存在
        OrderType existing = orderTypeMapper.selectById(id);
        if (existing == null) {
            return CommonResult.failed("未找到ID为" + id + "的订单类型");
        }
        int rows = orderTypeMapper.deleteById(id);
        return rows > 0 ? CommonResult.success("删除订单类型成功") : CommonResult.failed("删除订单类型失败");
    }

    /**
     * 实现查询所有订单类型的方法
     */
    @Override
    public CommonResult<List<OrderType>> getAll() {
        // 查询所有订单类型并按ID排序
        LambdaQueryWrapper<OrderType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(OrderType::getId);
        List<OrderType> orderTypeList = orderTypeMapper.selectList(queryWrapper);
        return CommonResult.success(orderTypeList);
    }
}
